<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;

class Record extends Model
{
    // 获取我投票的
    public static function oneForAll($map)
    {
        $list = Record::alias('r')->join('apply a', 'r.apply_id = a.id')
                ->join('user u', 'u.id = a.user_id')
                ->where($map)
                ->field('a.user_id, r.apply_id, count(*) as num, u.avatar, a.baby_name')
                ->group('r.apply_id')
                ->select();
        $total = 0;
        foreach($list as $key => $val) {
            $total += $val['num'];
        }
        return [
            'total' => $total,
            'list' => $list
        ];
    }

    // 获取投票给我的
    public static function allForOne($map)
    {
        $list = Record::alias('r')->join('user u', 'u.id = r.user_id')
            ->where($map)
            ->field('r.user_id, r.apply_id, count(*) as num, u.nickname, u.avatar')
            ->group('r.user_id')
            ->select();
        $total = 0;
        foreach($list as $key => $val) {
            $total += $val['num'];
        }
        return [
            'total' => $total,
            'list' => $list
        ];
    }

    public function add($data)
    {
        $data['create_time'] = time();
        return $this->insert($data);
    }
}
